﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace Config
{
    /// <summary>
    /// Xml配置类
    /// </summary>
    public class Xml
    {
        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public Xml() { }
        #endregion

        #region 配置文件变量
        /// <summary>
        /// 配置文件路径
        /// </summary>
        public static string ConfigPath = "Config.xml";

        #endregion

        #region 检测配置
        /// <summary>
        /// 检查配置
        /// </summary>
        /// <returns></returns>
        public static bool CheckConfig()
        {
            bool returns = false;
            try
            {
                if (IsFileExist(ConfigPath))
                {
                    returns = true;
                }
                else
                {
                    XElement rootNode = new XElement("Config");
                    rootNode.Save(ConfigPath);
                    returns = true;
                }
                return returns;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 日志配置
        /// <summary>
        /// 读取日志配置
        /// </summary>
        /// <returns></returns>
        public static Model.Log LogConfigRead()
        {
            Model.Log returns = new Model.Log();
            try
            {
                XDocument myXDoc = XDocument.Load(ConfigPath);
                XElement rootNode = myXDoc.Element("Config");
                foreach (XElement node in rootNode.Elements("LogConfig"))
                {
                    returns.LogPath =
                        Kernel.Encrypt.Des.DesDecrypt(node.Element("LogPath").Value);
                }
                //如果获取的日志配置为空,那写入默认配置
                if (returns.LogPath == null)
                {
                    XElement rootNode1 = XElement.Load(ConfigPath);
                    XElement newNode = new XElement("LogConfig",
                        new XElement("LogPath",
                            Kernel.Encrypt.Des.DesEncrypt("Log"))
                        );
                    returns.LogPath = "Log";
                    rootNode1.Add(newNode);
                    rootNode1.Save(ConfigPath);
                }
            }
            catch 
            {
                throw new Exception("载入日志配置出错!");
            }
            return returns;
        }

        /// <summary>
        /// 修改日志配置
        /// </summary>
        /// <param name="LogConfig"></param>
        /// <returns></returns>
        public static bool LogConfig(Model.Log LogConfig)
        {
            bool returns = false;
            try
            {
                XElement rootNode = XElement.Load(ConfigPath);
                IEnumerable<XElement> targetNodes = from target in rootNode.Descendants("LogConfig")
                                                    //where target.Attribute("Id").Value == ConfigModel.Id.ToString()
                                                    select target;
                foreach (XElement node in targetNodes)
                {
                    node.Element("LogPath").SetValue(
                        Kernel.Encrypt.Des.DesEncrypt(LogConfig.LogPath));
                }
                returns = true;
                rootNode.Save(ConfigPath);
            }
            catch
            {
                throw new Exception("修改日志配置出错!");
            }
            return returns;
        }
        #endregion

        #region 登陆配置
        /// <summary>
        /// 读取登陆配置
        /// </summary>
        /// <returns></returns>
        public static Model.Login LoginConfigRead()
        {
            Model.Login returns = new Model.Login();
            try
            {
                XDocument myXDoc = XDocument.Load(ConfigPath);
                XElement rootNode = myXDoc.Element("Config");
                foreach (XElement node in rootNode.Elements("Login"))
                {
                    returns.DataAddress =
                        Kernel.Encrypt.Des.DesDecrypt(node.Element("DataAddress").Value);
                    returns.DataAccounts =
                        Kernel.Encrypt.Des.DesDecrypt(node.Element("DataAccounts").Value);
                    returns.DataPassword =
                        Kernel.Encrypt.Des.DesDecrypt(node.Element("DataPassword").Value);
                    returns.AutoLogon =
                        System.Convert.ToInt32(Kernel.Encrypt.Des.DesDecrypt(node.Element("AutoLogon").Value));
                    returns.SavePassword =
                        System.Convert.ToInt32(Kernel.Encrypt.Des.DesDecrypt(node.Element("SavePassword").Value));
                    if (returns.SavePassword == 1)
                    {
                        returns.Checking = false;
                    }
                    else
                    {
                        returns.Checking = true;
                    }
                }
            }
            catch
            {
                throw new Exception("载入登陆配置出错!");
            }
            return returns;
        }

        /// <summary>
        /// 修改登陆配置
        /// </summary>
        /// <param name="LoginConfig"></param>
        /// <returns></returns>
        public static bool LoginConfig(Model.Login LoginConfig)
        {
            bool returns = false;
            try
            {
                XElement rootNode = XElement.Load(ConfigPath);
                IEnumerable<XElement> targetNodes = from target in rootNode.Descendants("Login")
                                                    //where target.Attribute("Id").Value == ConfigModel.Id.ToString()
                                                    select target;
                foreach (XElement node in targetNodes)
                {
                    node.Element("DataAddress").SetValue(
                        Kernel.Encrypt.Des.DesEncrypt(LoginConfig.DataAddress));
                    node.Element("DataAccounts").SetValue(
                        Kernel.Encrypt.Des.DesEncrypt(LoginConfig.DataAccounts));
                    node.Element("DataPassword").SetValue(
                        Kernel.Encrypt.Des.DesEncrypt(LoginConfig.DataPassword));
                    node.Element("Checking").SetValue(
                        Kernel.Encrypt.Des.DesEncrypt("1"));
                    node.Element("AutoLogon").SetValue(
                        Kernel.Encrypt.Des.DesEncrypt(LoginConfig.AutoLogon.ToString()));
                    node.Element("SavePassword").SetValue(
                        Kernel.Encrypt.Des.DesEncrypt(LoginConfig.SavePassword.ToString()));
                }
                returns = true;
                rootNode.Save(ConfigPath);
            }
            catch
            {
                throw new Exception("修改登陆配置出错!");
            }
            return returns;
        }
        #endregion

        #region 文件是否存在
        /// <summary>
        /// 文件是否存在(文件路径)
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns></returns>
        public static bool IsFileExist(string path)
        {
            if (File.Exists(path))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

    }
}
